﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using My.Admin.Dto.System;
using My.Admin.Enum;
using My.Admin.Model;
using My.Admin.Model.System;
using My.Admin.Repository;
using My.Admin.Service.System.IService;
using My.Core.CustomException;
using My.Core.Enums;
using My.Core.SqlSugar.model;
using SqlSugar.IOC;
using System.ComponentModel;
using System;

namespace My.Admin.Service.System
{
	public class SysDictDataService : BaseService<SysDictData>, ISysDictDataService
	{
		/// <summary>
		/// 获取字典值分页列表
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<PagedInfo<SysDictData>> Page(PageDictDataInput input)
		{
			var code = !string.IsNullOrEmpty(input.Code?.Trim());
			var value = !string.IsNullOrEmpty(input.Value?.Trim());
			var lSysDictData = AsQueryable()
				.Where(u => u.DictTypeId == input.DictTypeId)
				.WhereIF(code, u => u.Code.Contains(input.Code))
				.WhereIF(value, u => u.Value.Contains(input.Value))
				.OrderBy(u => new { u.OrderNo, u.Code });
			return await lSysDictData.ToPageAsync(input);
		}

		/// <summary>
		/// 获取字典值列表
		/// </summary>
		/// <returns></returns>
		public async Task<List<SysDictData>> GetList([FromQuery] GetDataDictDataInput input)
		{
			return await GetDictDataListByDictTypeId(input.DictTypeId);
		}

		/// <summary>
		/// 增加字典值
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task AddDictData(AddDictDataInput input)
		{
			var isExist = await IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId);
			if (isExist)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典值已存在,名称或编码重复", "字典值已存在,名称或编码重复");
			await InsertAsync(input.Adapt<SysDictData>());
		}

		/// <summary>
		/// 更新字典值
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task UpdateDictData(UpdateDictDataInput input)
		{
			var isExist = await IsAnyAsync(u => u.Id == input.Id);
			if (!isExist) throw new CustomException(ResultCode.CUSTOM_ERROR, "字典值不存在", "字典值不存在");

			isExist = await IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
			if (isExist) throw new CustomException(ResultCode.CUSTOM_ERROR, "字典值已存在,名称或编码重复", "字典值已存在,名称或编码重复");

			await UpdateAsync(input.Adapt<SysDictData>());
		}

		/// <summary>
		/// 删除字典值
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task DeleteDictData(DeleteDictDataInput input)
		{
			var dictData = await GetFirstAsync(u => u.Id == input.Id);
			if (dictData == null)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典值不存在", "字典值不存在");

			await DeleteAsync(dictData);
		}

		/// <summary>
		/// 获取字典值详情
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<SysDictData> GetDetail([FromQuery] DictDataInput input)
		{
			return await GetFirstAsync(u => u.Id == input.Id);
		}

		/// <summary>
		/// 修改字典值状态
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task SetStatus(DictDataInput input)
		{
			var dictData = await GetFirstAsync(u => u.Id == input.Id);
			if (dictData == null)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典值不存在", "字典值不存在");

			//if (!Enum.IsDefined(typeof(StatusEnum), input.Status))
			//	throw new CustomException(ResultCode.CUSTOM_ERROR, "字典状态错误", "字典状态错误");

			dictData.Status = (StatusEnum)input.Status;
			await UpdateAsync(dictData);
		}

		/// <summary>
		/// 根据字典类型Id获取字典值集合
		/// </summary>
		/// <param name="dictTypeId"></param>
		/// <returns></returns>
		[NonAction]
		public async Task<List<SysDictData>> GetDictDataListByDictTypeId(long dictTypeId)
		{
			return await AsQueryable()
				.Where(u => u.DictTypeId == dictTypeId)
				.OrderBy(u => new { u.OrderNo, u.Code })
				.ToListAsync();
		}

		/// <summary>
		/// 根据字典类型编码获取字典值集合
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		public async Task<List<SysDictData>> GetDataList(string code)
		{
			return await Context.Queryable<SysDictType>()
				.LeftJoin<SysDictData>((u, a) => u.Id == a.DictTypeId)
				.Where((u, a) => u.Code == code && u.Status == StatusEnum.Enable && a.Status == StatusEnum.Enable)
				.OrderBy((u, a) => new { a.OrderNo, a.Code })
				.Select((u, a) => a).ToListAsync();
		}

		/// <summary>
		/// 根据查询条件获取字典值集合
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<List<SysDictData>> GetDataList([FromQuery] QueryDictDataInput input)
		{
			return await Context.Queryable<SysDictType>()
				.LeftJoin<SysDictData>((u, a) => u.Id == a.DictTypeId)
				.Where((u, a) => u.Code == input.Code)
				.WhereIF(input.Status.HasValue, (u, a) => a.Status == (StatusEnum)input.Status.Value)
				.OrderBy((u, a) => new { a.OrderNo, a.Code })
				.Select((u, a) => a).ToListAsync();
		}

		/// <summary>
		/// 根据字典类型Id删除字典值
		/// </summary>
		/// <param name="dictTypeId"></param>
		/// <returns></returns>
		[NonAction]
		public async Task DeleteDictData(long dictTypeId)
		{
			await DeleteAsync(u => u.DictTypeId == dictTypeId);
		}
	}
}
